package com.kcbsdk;

import android.util.Log;

import com.kcbsdk.MyDomain.ADomainUser;
import com.kcbsdk.MyDomain.AListPage;
import com.kcbsdk.MyTools.ACloudInfo;
import com.kcbsdk.MyTools.ACloudType;
import com.kcbsdk.MyTools.AFileType;
import com.kcbsdk.MyTools.AIconTypeList;
import com.kcbsdk.MyTools.ALocalInfoList;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/4/1.
 */

public class CSvrTools implements wpapper {
    public int g4EventMethod;
    public int g4Test_0x21;

    public int pollingEvent(){
        return g4EventMethod | cjmkcs.systemPollingEvent(tTOOLS, 0);
    }
    public void setMethodType(){
//	MSTRING("setMethodType");
        cjmkcs.systemSetMethodType(tTOOLS | 0x00000001);
    }
    public int getMethodType(){
        return cjmkcs.systemSetMethodType(tTOOLS);
    }
    public String getMethodMessage(int type){
        return cjmkcs.systemGetMethodMessage(type);
    }
    public void wrMemString(String key, String text){
        cjmkcs.systemSetMemoryString(key, text);
    }
    public void wrMemValue(String key, int value){
        cjmkcs.systemSetMemoryString(key, String.format("%d", value));
    }
    public void wrMemBoolean(String key, boolean flag){
        cjmkcs.systemSetMemoryString(key, flag ? "1" : "0");
    }
    public String rdMemString(String key){
        return cjmkcs.systemGetMemoryString(key);
    }
    public int rdMemValue(String key){
        String gsLocal_1 = cjmkcs.systemGetMemoryString(key);
        if (null != gsLocal_1 && !"".equals(gsLocal_1)) {
            return Integer.valueOf(gsLocal_1);
        }
        return 0;
    }
    public boolean rdMemBoolean(String key){
        String gsLocal_1 = cjmkcs.systemGetMemoryString(key);
        return "1".equals(gsLocal_1);
    }
    public void setDebugFlag(int debugFlag, int loadDelay){
        cjmkcs.systemSetDebugFlag(debugFlag, loadDelay);
    }

    public void fetchHeadPhoto(int userID, int aImageSize, int dateTime) {
    }
    public void fetchPhotoFile(int userID, int number, int dateTime){
        // cloudType 0=K吧 1=原唱吧 2=伴奏吧 3=图吧 4=文档
        // 当cloudType为图吧时 fileType为dateTime，其他的 0=MP3 1=MP4 2=文件详情
      //  cjmkcs.systemDownloadCloudFile(userID, number, dateTime, 0, "", TOOLS_DOWNLOAD_CLOUD_FILE);
    }
    public String eventFetchPhoto(){
        String string = cjmkcs.systemGetWpapperString(wpTOOLS_FetchPhoto);
        return string;
    }

    public void upLoadPhotoFile(String mix, String imgFile) {
//        cjmkcs.toolsUploadPhotoFile(mix, imgFile, TOOLS_UPLOAD_PHOTE_FILE);
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 本地队列操作 ////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public void optLocalInfoList(int type, int cloudFileType, int userID, int fileID, String songName, String intro, String mixText, String fileName){
        cloudFileType |= g4Test_0x21 << 8;
        cjmkcs.systemLocalInfoList(type, cloudFileType, userID, fileID, songName, intro, mixText, fileName, TOOLS_FETCH_DBS_LIST);
    }
    public ALocalInfoList[] eventLocalInfoList(int methodType){
        ALocalInfoList[] aLocalInfoList = null;
        int number = (methodType >> 16) & 0xff;
        int indexQty = cjmkcs.systemDatabaseListIndex(0, number, 0);
        if (indexQty > 0){
            aLocalInfoList = new ALocalInfoList[indexQty];
            int g4Local_1;
            int uiPageName = (methodType >> 8) & 0x3f;
            for (int counter = 0; counter < indexQty; counter++){
                MSTRING(String.format("eventLocalInfoList A %d %d %d %d", uiPageName, number, counter, indexQty));
                cjmkcs.systemDatabaseListIndex(2, number, counter);
                aLocalInfoList[counter] = new ALocalInfoList();
                g4Local_1 = cjmkcs.systemGetWpapperValue(wpTOOLS_DbsFlag0 | number);	// [0-1]长度[2-3]itemID
                aLocalInfoList[counter].IndexID = (g4Local_1>>16) & 0xffff;

                g4Local_1 = cjmkcs.systemGetWpapperValue(wpTOOLS_DbsFlag1 | number);	// [4]状态0=没有使用1=使用3=完成[5]uiPageName[6]fileType[7]cloudType
                aLocalInfoList[counter].IsComplete = ((g4Local_1 & 0xff) == 3) ? true : false;	// [0]=1使用、2=传输中，3=传输完成
                aLocalInfoList[counter].IsLoaging = ((g4Local_1 & 0xff) == 2) ? true : false;	// [0]=1使用、2=传输中，3=传输完成
                aLocalInfoList[counter].IsVideo = (((g4Local_1 >> 16) & 0xff) == TOOLS_CLOUD_FILE_VIDEO)? true : false;	// [1]AFileType

                aLocalInfoList[counter].MusicID0 = cjmkcs.systemGetWpapperValue(wpTOOLS_DbsMusicID0 | number);
                aLocalInfoList[counter].MusicID1 = cjmkcs.systemGetWpapperValue(wpTOOLS_DbsMusicID1 | number);
                aLocalInfoList[counter].FileSize = cjmkcs.systemGetWpapperValue(wpTOOLS_DbsFileSize | number);
                aLocalInfoList[counter].AreaCode = cjmkcs.systemGetWpapperValue(wpTOOLS_DbsAreaCode | number);

                // [U 0][U 1][U 2][U 3][U 4][A 5][A 6]
                aLocalInfoList[counter].SongName = cjmkcs.systemGetWpapperString(wpTOOLS_DbsUString0 | number);

                if (uiPageName == TOOLS_UI_PAGE_DOWN_OBBLIG){
                    // 下载伴奏时	[U歌曲名][U介绍][U合成文字][A文件路径]
                    aLocalInfoList[counter].ArtistName = cjmkcs.systemGetWpapperString(wpTOOLS_DbsUString1 | number);
                    aLocalInfoList[counter].IconFile = cjmkcs.systemGetWpapperString(wpTOOLS_DbsIconFile | number);
                }
                else if (uiPageName == TOOLS_UI_PAGE_UP_RECORD){
                    // 上传录音时	[U歌曲名][U介绍][U合成文字][A文件路径]
                    aLocalInfoList[counter].SongIntro = cjmkcs.systemGetWpapperString(wpTOOLS_DbsUString1 | number);
                    aLocalInfoList[counter].IconFile = cjmkcs.systemGetWpapperString(wpTOOLS_DbsIconFile | number);
                    aLocalInfoList[counter].RecordCoverFile = cjmkcs.systemGetWpapperString(wpTOOLS_DbsCoverFile | number);
                    aLocalInfoList[counter].RecordAudioFile = cjmkcs.systemGetWpapperString(wpTOOLS_DbsAudioFile | number);
                    aLocalInfoList[counter].RecordVideoFile = cjmkcs.systemGetWpapperString(wpTOOLS_DbsVideoFile | number);
                }
                else {
                    // 查找手机伴奏 文件时  [U歌曲名][U歌手名][U文件名][U路径]
                    aLocalInfoList[counter].ArtistName = cjmkcs.systemGetWpapperString(wpTOOLS_DbsUString1 | number);
                    aLocalInfoList[counter].FileName = cjmkcs.systemGetWpapperString(wpTOOLS_DbsUString2 | number);
                    aLocalInfoList[counter].FilePath = cjmkcs.systemGetWpapperString(wpTOOLS_DbsUString3 | number);
                }
            }
        }
        cjmkcs.systemDatabaseListIndex(3, number, 0);
        return aLocalInfoList;
    }
    public int eventLoadingFile(int uiPageName, ALocalInfoList[] aLocalInfoList, int listLength){
        int g4Local_1;
        int g4Local_2;
//MDEBUG(0xd3);
        for (int counter = 0; counter < 16; counter++){
            g4Local_1 = cjmkcs.systemGetWpapperValue(wpTOOLS_LoadRunType | counter);
            if (g4Local_1 > 0){
                if (uiPageName == ((g4Local_1 >> 16) & 0xff) /*&& (g4Local_1 & 0xffff0000) == (aLocalInfoList[0].Type & 0xffff0000)*/){
                    MSTRING("eventLoadingFile B");
                    for (g4Local_2 = 0; g4Local_2 < listLength; g4Local_2++){
                        if (aLocalInfoList[g4Local_2].IndexID == cjmkcs.systemGetWpapperValue(wpTOOLS_LoadRunItemID | counter)){				// 本地信息ID
                            aLocalInfoList[g4Local_2].FileLength = cjmkcs.systemGetWpapperValue(wpTOOLS_LoadRunLength | counter);			// 文件字节长度
                            aLocalInfoList[g4Local_2].FileCounter = cjmkcs.systemGetWpapperValue(wpTOOLS_LoadRunCounter | counter);			// 文件已经传输字节
                            MSTRING(String.format("eventLoadingFile C %d %d %d %d",counter,g4Local_2,aLocalInfoList[g4Local_2].FileCounter,aLocalInfoList[g4Local_2].FileLength));
//MDEBUG(0xa5);
                            return getUiPageNameEvent(g4Local_1);
                        }
                    }
                }
            }
        }
        return 0;
    }
    public int getUiPageNameEvent(int uiPageName){
        switch (uiPageName & 0xff){
            case TOOLS_UI_PAGE_LOCAL_OBBLIG: return 0x00000001;
            case TOOLS_UI_PAGE_LOCAL_MUSIC: return 0x00000002;
            case TOOLS_UI_PAGE_LOCAL_PHOTO: return 0x00000004;
            case TOOLS_UI_PAGE_LOCAL_ANY_FILE: return 0x00000008;
            case TOOLS_UI_PAGE_DOWN_OBBLIG: return 0x00000100;
            case TOOLS_UI_PAGE_UP_RECORD: return 0x0000200;
            case TOOLS_UI_PAGE_READY_SING: return 0x0000400;
            case TOOLS_UI_PAGE_DOWN_FILE: return 0x0000800;
            case TOOLS_UI_PAGE_UP_FILE: return 0x0001000;
        }
        return 0x00000000;
    }


    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 获取图标列表操作 /////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public void fetchIconTypeList(int cloudType, int readType, int iconType, String name){		// 获取图标列表
        cloudType |= g4Test_0x21;
        int g4Local_1 = (readType << 14) | ((cloudType & 0x3f) << 8) | TOOLS_DBS_LIST_ICON_TYPE;
        MSTRING(String .format("fetchIconTypeList %08x", g4Local_1));
        cjmkcs.systemGetDatabaseList(g4Local_1, iconType, 0, 0, name, TOOLS_FETCH_DBS_LIST);
    }
    public AIconTypeList[] methodIconTypeListDbs(int methodType, int iconType){
        AIconTypeList[] aIconTypeList = null;
        int number = (methodType >> 16) & 0xff;
        int indexQty = cjmkcs.systemDatabaseListIndex(0, number, 0);
        if (indexQty > 0){
            aIconTypeList = new AIconTypeList[indexQty];
            for (int counter = 0; counter < indexQty; counter++) {
                cjmkcs.systemDatabaseListIndex(2, number, counter);
                aIconTypeList[counter] = new AIconTypeList();
                aIconTypeList[counter].TypeID = cjmkcs.systemGetWpapperValue(wpTOOLS_DbsTypeID | number);
                aIconTypeList[counter].TypeName = cjmkcs.systemGetWpapperString(wpTOOLS_DbsTypeName | number);
                aIconTypeList[counter].PinYin = cjmkcs.systemGetWpapperString(wpTOOLS_DbsPinYin | number);
                aIconTypeList[counter].IconFile = cjmkcs.systemGetWpapperString(wpTOOLS_DbsIconFile | number);
            }
        }
        cjmkcs.systemDatabaseListIndex(3, number, 0);
        return aIconTypeList;
    }

    public void fetchCloudInfoList(int readType, int cloudType, int userID){
        cjmkcs.systemGetDatabaseList((readType << 14) | (cloudType << 8) | TOOLS_DBS_LIST_CLOUD, userID, 0, 0, "", TOOLS_FETCH_DBS_LIST);
    }
    public void downloadCloudFile(int userID, int fileID, int cloudType, int fileType, String pageMark){
      //  cjmkcs.systemDownloadCloudFile(userID, fileID, cloudType, fileType, pageMark, TOOLS_DOWNLOAD_CLOUD_FILE);
    }
    public void uploadCloudFile(int cloudType, String loadingMark, String mixText, String fileName){
        // mixText [0]文件名[1]用户名[2]文件介绍[3]MP3文件[4]歌词文件
    //    cjmkcs.systemUploadCloudFile(cloudType, loadingMark, mixText, fileName, TOOLS_UPLOAD_CLOUD_FILE);
    }
    public void deleteCloudFile(int userID, int fileID, int cloudType) {
        cjmkcs.systemDeleteCloudFile(userID, fileID, cloudType, TOOLS_DELETE_CLOUD_FILE);
    }
    public void releaseCloudFile(int userID, int fileID, int cloudType, int areaData, String intro) {
       // int type = ((cloudType & 0xff) << 16) | (areaData & 0xffff);
        cjmkcs.systemReleaseCloudFile(userID, fileID, cloudType, areaData, intro, TOOLS_RELEASE_CLOUD_FILE);
    }
    public String writeKcbCommand(int type, int subType, int length, byte[] in_data, byte[] out_data) {
        return cjmkcs.systemWriteKcbCommand(type, subType, length, in_data, out_data, TOOLS_WRITE_KCB_COMMAND);
    }
    public String writeNetCommand(int type, String gsLocal_1, String gsLocal_2, String gsLocal_3) {
        return cjmkcs.systemWriteNetCommand(type, gsLocal_1, gsLocal_2, gsLocal_3, TOOLS_WRITE_NET_COMMAND);
    }
    public void getAuditItem(int readType) {
        cjmkcs.systemGetAuditItem(readType, TOOLS_GET_AUDIT);
    }
    public void setAuditItem(boolean isPassed) {
        cjmkcs.systemSetAuditItem(isPassed ? 1 : 0, TOOLS_SET_AUDIT);
    }
    public void sendSuggestion(String suggestion, String contacts) {
        cjmkcs.systemSendSuggestion(suggestion, contacts, TOOLS_SUGGESTION);
    }
    public void setLyricScreen(int line, int columnPixel){
        cjmkcs.systemSetLyricScreen(line, columnPixel);
    }



    public AListPage methodCloudInfoDbs(int type, int fileNumber){
//        g4EventMethod |= getNumberEvent((type >> 8) & 0x0f);
//        if (cjmkcs.systemLoadDbsListFile(type & 0x0fff, 0, 0, 0, 0) > 0){
//            return null;
//        }
//        int indexCounter = cjmkcs.systemGetWpapperValue(wpTOOLS_DbsIndexQty);
//       MSTRING(String.format("methodCloudInfoDbs %02x %d", type, indexCounter));
//        List<ACloudInfo> aCloudInfos = new ArrayList<>();
//        for (int counter = 0; counter < indexCounter; counter++) {
//            cjmkcs.systemGetWpapperValue(wpTOOLS_DbsLoadIndex | counter);
//            ACloudInfo aCloudInfo = new ACloudInfo();
//            aCloudInfo.FileID = cjmkcs.systemGetWpapperValue(wpTOOLS_CloudDbsFileID);
////MDWORD(aCloudInfo.FileID);
//            aCloudInfo.FileSize = cjmkcs.systemGetWpapperValue(wpTOOLS_CloudDbsFileSize);
////MDWORD(aCloudInfo.FileSize);
//            aCloudInfo.FileInfo = cjmkcs.systemGetWpapperValue(wpTOOLS_CloudDbsFileInfo);
//            aCloudInfo.IsVideo = (aCloudInfo.FileInfo & 0x01)>0?true:false;
//            aCloudInfo.CommentQty = cjmkcs.systemGetWpapperValue(wpTOOLS_CloudDbsCommentQty);
//            aCloudInfo.ListenQty = cjmkcs.systemGetWpapperValue(wpTOOLS_CloudDbsListenQty);
//            aCloudInfo.FlowerQty = cjmkcs.systemGetWpapperValue(wpTOOLS_CloudDbsFlowerQty);
//            aCloudInfo.ShareQty = cjmkcs.systemGetWpapperValue(wpTOOLS_CloudDbsShareQty);
//            aCloudInfo.RankListNumber = cjmkcs.systemGetWpapperValue(wpTOOLS_CloudDbsRankListNum);
//            aCloudInfo.RankAreaData = cjmkcs.systemGetWpapperValue(wpTOOLS_CloudDbsRankAreaData);
//            aCloudInfo.LyricFile = cjmkcs.systemGetWpapperValue(wpTOOLS_CloudDbsLyricFile);
//
//            aCloudInfo.FileDate = cjmkcs.systemGetWpapperString(wpTOOLS_CloudDbsFileDate);
//            aCloudInfo.ReleaseDate = cjmkcs.systemGetWpapperString(wpTOOLS_CloudDbsReleaseDate);
//            aCloudInfo.FileName = cjmkcs.systemGetWpapperString(wpTOOLS_CloudDbsFileName);
//            aCloudInfo.FileIntro = cjmkcs.systemGetWpapperString(wpTOOLS_CloudDbsFileIntro);
//            aCloudInfo.RankName = cjmkcs.systemGetWpapperString(wpTOOLS_CloudDbsRankName);
//            aCloudInfo.SrcSinger = cjmkcs.systemGetWpapperString(wpTOOLS_CloudDbsSrcSinger);
//            MSTRING(aCloudInfo.FileDate);
//            MSTRING(aCloudInfo.FileName);
//            MSTRING(aCloudInfo.FileIntro);
//            aCloudInfos.add(aCloudInfo);
//        }
//        AListPage newListPage = new AListPage();
//        newListPage.Counter = indexCounter;newListPage.Total = cjmkcs.systemGetWpapperValue(wpTOOLS_DbsIndexMax);
//        newListPage.Result = aCloudInfos;
//        return newListPage;
        return null;
    }
    private final static int[] Tab_CounterEvent = new int[]{
            TOOLS_EVENT_DBS_MEDIA_KBA, TOOLS_EVENT_DBS_MEDIA_SRC, TOOLS_EVENT_DBS_MEDIA_BAND, TOOLS_EVENT_DBS_PHOTO, TOOLS_EVENT_DBS_DOCUMENTS,
    };
    public int getNumberEvent(int number){
        return Tab_CounterEvent[number % 5];
    }



    public void MSTRING(String text) {
        Log.d("MSYS", text);
    }

}
